class_name HoldLightningStaff
extends HoldMagic


@onready var marker_2d: Marker2D = $Marker2D
const LIGHTNING_MAGIC = preload("uid://3g6vgb46mqf3")
@export var lightning_dist: float = 48


func use():
	shoot_lightning(Vector2.ZERO)


func shoot_lightning(offset: Vector2):
	var player = holder as Player
	player.level.emit_particle("particle_shoot_lightning", marker_2d.global_position)
	AudioManager.play_sound("sfx_lightning_shoot")
	
	var result = GlobalUtils.raycast_to(player.level.cross_hair.global_position + offset, player.level.cross_hair.global_position + offset + Vector2.DOWN * 1000, 1)
	if result:
		await get_tree().create_timer(0.2).timeout
		
		var magic = LIGHTNING_MAGIC.instantiate() as LightningMagic
		
		var config = item_data.item_config as MagicStaffConfig
		magic.attack = config.attack + player.player_data.get_attack()
		magic.critical_rate = config.critical_rate
		magic.critical_bonus = config.critical_bonus
		magic.knockback = config.knockback
		magic.sender = holder
		
		player.level.particles_parent.add_child(magic)
		magic.global_position = result["position"]
		magic.set_is_enemy(false)
		
		#if config.cam_shake:
			#player.level.camera.start_shake(config.cam_shake_intensity, config.cam_shake_duration)
	
	
